﻿using System;
using System.Collections;
using System.Collections.Generic;
using OWL.Rendering.HRP;
using Yoozoo.Framework.Managers;

namespace Yoozoo.Core.Common
{
    /// <summary>
    /// 着色器分级管理
    /// </summary>
    public class ShaderQualityListener :  
        QualityListenerMono<ShaderQualityListener.ShaderQualitySetting>,
        IShaderLevelAdjuster,
        IPostEffectAdjuster
    {
        [Serializable]
        public class ShaderQualitySetting : QualitySetting
        {
            public CharacterLODLevel characterLodLevel;
            public SceneLODLevel sceneLodLevel;
            
            public void Apply()
            {
                if (HRenderSceneManager.Inst)
                {
                    List<HGradeData> hGradeDatas = HRenderSceneManager.Inst.hGradDatas;
                    if (hGradeDatas!=null)
                    {
                        foreach (var gradeData in hGradeDatas)
                        {
                            gradeData.characterLODLevel = characterLodLevel;
                            gradeData.sceneLODLevel = sceneLodLevel;
                            gradeData.Sync(true);
                        }
                    }
                }
               
            }
        }

        public int characterGraphicLevel = -1;
        public int sceneGraphicLevel = -1;
        public int fxEffectGraphicLevel = -1;
        public int postEffectLevel = -1;
        
        protected override void Awake()
        {
            HRenderSceneManager.onAddGradeData += OnAddGradeData;
        }

        private void OnAddGradeData(HGradeData obj)
        {
            if (CurrentQualitySetting!=null)
            {
                ChangeQualitySetting(CurrentQualitySetting);
                ChangeLevelSetting();
            }
        }
        
        public override void ChangeQualitySetting(ShaderQualitySetting qualitySetting)
        {
            qualitySetting.Apply();
        }
        
        private void ChangeLevelSetting()
        {
            List<HGradeData> hGradeDatas = HRenderSceneManager.Inst.hGradDatas;
            if (hGradeDatas!=null)
            {
                foreach (var gradeData in hGradeDatas)
                {
                    if (characterGraphicLevel >= 0)
                    {
                        if (characterGraphicLevel == 0)//合并角色和场景材质分级：min和low
                        {
                            gradeData.characterGraphicLevel = (CharacterGraphicLevel)1;
                        }
                        else 
                        { 
                            gradeData.characterGraphicLevel = (CharacterGraphicLevel)characterGraphicLevel; 
                        }
                    }
                    if (sceneGraphicLevel >= 0) 
                    {
                        if (sceneGraphicLevel == 0)
                        {
                            gradeData.sceneGraphicLevel = (SceneGraphicLevel)1;
                        }
                        else {
                            gradeData.sceneGraphicLevel = (SceneGraphicLevel)sceneGraphicLevel;
                        }
                    }
                    if(fxEffectGraphicLevel>=0) gradeData.fXGraphicLevel = (FXGraphicLevel)fxEffectGraphicLevel;
                    if (postEffectLevel >= 0) gradeData.postProcessGraphicLevel = (PostProcessGraphicLevel)postEffectLevel;
                    gradeData.Sync(true);
                }
                ShaderMgr.OnQualityChanged();
            }
        }

        public void ChangeCharacterGraphicLevel(int level)
        {
            characterGraphicLevel = level;
            ChangeLevelSetting();
        }

        public void ChangeSceneGraphicLevel(int level)
        {
            sceneGraphicLevel = level;
            ChangeLevelSetting();
        }

        public void ChangeFxEffectLevel(int level)
        {
            fxEffectGraphicLevel = level;
            ChangeLevelSetting();
        }

        public void ChangePostEffectStyle(int style)
        {
            
        }

        public void ChangePostEffectLevel(int level)
        {
            postEffectLevel = level;
            ChangeLevelSetting();
        }
        
        public List<ShaderQualitySetting> GetAllQualitySettings()
        {
            return QualitySettings;
        }
    }
}
